package com.wuxinwudai.android.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.wuxinwudai.android.R;


/**
 * BaseActivity 类为本应用 Activity 通用基类
 * @author  吾心无待 于 2015年09月10日.
 */
public class BaseActivity extends AppCompatActivity {
    protected final String TAG = this.getClass().getCanonicalName();
    protected boolean mInOutAnim = true;//出入动画

    /**
     * 获取是否执行 Activity 进出动画
     * @return 是否执行 Activity 进出动画
     */
    public boolean getInOutAnim() {
        return mInOutAnim;
    }

    /**
     * 设置是否执行 Activity 进出动画
     * @param inOutAnim 是否执行 Activity 进进出动画
     */
    public void setInOutAnim(boolean inOutAnim) {
        this.mInOutAnim = inOutAnim;
    }

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        if (mInOutAnim){
            overridePendingTransition(R.anim.slide_in_right_to_left, R.anim.slide_out_right_to_left);
        }
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    public void finish() {
        super.finish();
        if (mInOutAnim){
            overridePendingTransition(R.anim.slide_in_left_to_right,R.anim.slide_out_left_to_right);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mInOutAnim){
            overridePendingTransition(R.anim.slide_in_left_to_right,R.anim.slide_out_left_to_right);
        }
    }

    /**
     * Activity 下查找View
     *
     * @param id   控件的id
     * @param <VT> View类型
     * @return 子视图 View
     */
    protected <VT extends View> VT getViewById(@IdRes int id) {
        return (VT) findViewById(id);
    }

    /**
     * 用户输出 info 信息
     * @param msg 信息
     */
    protected void i(String msg){
        Log.i(TAG,msg);
    }
}
